package org.dk.controller;


import com.alibaba.fastjson.JSONObject;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQPushConsumerLifecycleListener;
import org.dk.service.itempageservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import java.util.List;


@Component
@RocketMQMessageListener(topic = "item-goods",consumerGroup = "item_goods_ids")
public class ItemPageController implements RocketMQListener<String>,RocketMQPushConsumerLifecycleListener {


    @Autowired
    private itempageservice service;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void prepareStart(DefaultMQPushConsumer defaultMQPushConsumer) {
        //MessageListenerConcurrently普通消息 MessageListenerOrderly 顺序消息
        defaultMQPushConsumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                //获取消息
                MessageExt messageExt = list.get(0);
                //重试次数
                int reconsumeTimes = messageExt.getReconsumeTimes();
                System.out.println(reconsumeTimes);
                //消息id
                String msgId = messageExt.getMsgId();
                System.out.println(msgId);
                try {
                    Object mq = redisTemplate.boundHashOps("mq").get(msgId);
                    if (mq == null){
                        //主题
                        String topic = messageExt.getTopic();
                        System.out.println(topic);
                        //消息内容
                        String s = new String(messageExt.getBody());
                        Long[] longs = JSONObject.parseObject(s, Long[].class);
                        for (Long aLong : longs) {
                            service.itempage(aLong);
                        }
                        System.out.println("消费成功");
                        redisTemplate.boundHashOps("mq").put(msgId,"1");
                    }else{
                        System.out.println("重复消费");
                        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                    }
                }catch (Exception e){
                    //重试次数大于三次直接返回
                    if(reconsumeTimes >= 3){
                        return ConsumeConcurrentlyStatus.RECONSUME_LATER;
                    }
                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
    }
    //此方法没有返回值,不能解决幂等性问题以及私信队列问题
    @Override
    public void onMessage(String s) {

    }

    /*public static void main(String[] args) throws IOException, TemplateException {
        //第一步创建一个Configuration对象
        Configuration configuration = new Configuration(Configuration.getVersion());
        //第二步设置模板文件所在的路劲
        configuration.setDirectoryForTemplateLoading(new File("D:\\idea\\dongdongshop_paret2\\dongdongshop_freemarker_service\\src\\main\\resources"));
        //第三部设置模板使用的字符集一般是utf-8
        configuration.setDefaultEncoding("utf-8");
        //第四部加载一个模板创建一个模板对象
        Template template = configuration.getTemplate("freemarker/text.ftl");
        //第五步创建一个模板使用的数据集一般是map也可使pojo通常使用的是map
        Map mao = new HashMap();
        //创建一个writer对象指定生成文件名
        FileWriter fileWriter = new FileWriter("F:\\item_page\\text.html");
        //第七部调用模板对象的process方法输出文件并传递数据map
        template.process(mao,fileWriter);
        //最后关流
        fileWriter.close();
    }
*/
/*  @RequestMapping("intempage")
    @ResponseBody
    public String intempage(Long id){
        service.itempage(id);
        return "ok";
    }*/
}
